<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/23
 * Time: 14:32
 */
namespace app\admin\model;
use think\Model;

class Roles extends Model
{
    protected $name = 'roles';
    protected $id = 'id';
    public function __construct($data = [])
    {
        if(session('modular')){
            $this->name = session('modular').'_'.$this->name;
        }else{
            $mokuai = request()->module();
            $this->name = $mokuai.'_'.$this->name;
        }
        parent::__construct($data);
    }

    /**检查是否有权限改变
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function isMyPid($id){
        $roles = Roles::where('id',$id)->json(['role'])->findOrEmpty()->toArray();
        if(!$user = session('adminuser')){
            return false;
        }
        $myrole = Roles::where('id',$user['role_id'])->json(['role'])->findOrEmpty()->toArray();
        if(count($myrole['role'])<count($roles['role'])){
            return false;
        }
        foreach ($roles['role'] as $value){
            if(!in_array($value,$myrole['role'])){
                return false;
            }
        }
        return true;
    }
}